package funge {
	import flash.geom.Point;
	
	public class Cursor {
		public static const UP:int = 0;
		public static const DOWN:int = 1;
		public static const LEFT:int = 2;
		public static const RIGHT:int = 3;
		
		private var _x:int;
		private var _y:int;
		private var _direction:int;
		private var _jumped:Boolean;
		
		private var _programWidth:int;
		private var _programHeight:int;
		
		public function Cursor(widthIn:int, heightIn:int) {
			_x = 0;
			_y = 0;
			_direction = RIGHT;
			_jumped = false;
			
			_programWidth = widthIn;
			_programHeight = heightIn;
		}
		
		public function changeDirection(direction:int):void {
			_direction = direction;
			if (_direction > RIGHT || _direction < UP)
				throw new Error("Cursor._direction invalid!");
		}
		
		public function update():void {
			//do not update as normal this cycle if a jump has been made
			if (_jumped) {
				_jumped = false;
				return;
			}
			
			switch (_direction) {
				case UP:
					_y --;
					break;
				case DOWN:
					_y ++;
					break;
				case LEFT:
					_x --;
					break;
				case RIGHT:
					_x ++;
					break;
			}
			wrap();
		}
		
		public function jump(jumpX:int, jumpY:int):void {
			_x = jumpX;
			_y = jumpY;
			_jumped = true;
			wrap();
		}
		
		private function wrap():void {
			while (_y < 0)
				_y += _programHeight;
			while (_y >= _programHeight)
				_y %= _programHeight;
			
			while (_x < 0)
				_x += _programWidth;
			while (_x >= _programWidth)
				_x %= _programWidth;			
		}
		
		public function get x():int						{return _x}
		public function get y():int						{return _y;}
		
//		public function set x(xIn:int):void					{_x = xIn;}
//		public function set y(yIn:int):void					{_y = yIn;}
//		
//		public function set jumped(jumpedIn:Boolean):void	{_jumped = jumpedIn;}
	}
}
